using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SensorAngleSync : MonoBehaviour
{
    AngleSensorRequest AglSesrR;
    Transform shouwan;
    Transform xiabi;
    Transform shangbi0;
    Transform shangbi1;
    Transform zhouguanjie1;

    Transform jianxuanzhuan;
    Transform zhouguanjie0;
    InputField[] inputFields;

    void Start()
    {
        AglSesrR = GetComponent<AngleSensorRequest>();
        shouwan = GameObject.Find("shouwan").GetComponent<Transform>();
        xiabi = GameObject.Find("xiabi").GetComponent<Transform>();
        shangbi0 = GameObject.Find("shangbi0").GetComponent<Transform>();
        shangbi1 = GameObject.Find("shangbi1").GetComponent<Transform>();
        zhouguanjie1 = GameObject.Find("zhouguanjie1").GetComponent<Transform>();
        jianxuanzhuan = GameObject.Find("jianxuanzhuan").GetComponent<Transform>();
        zhouguanjie0 = GameObject.Find("zhouguanjie0").GetComponent<Transform>();
        AglSesrR.SetSensorAngleHandle(AngleSensorCallback);
        inputFields = GameObject.Find("adjust").GetComponentsInChildren<InputField>();
        foreach (var item in inputFields)
        {
            item.text = "0";
        }
    }

    // Update is called once per frame
    void Update()
    {
        AglSesrR.RequstSensorAngle(AngleSensorRequest.SENSOR_ID.SENSOR_ALL);
        //  Debug.Log("test");
    }


    private void AngleSensorCallback(object id, object obj)
    {
        int ID = 0;
        double value = 0;
        if (id is string)
        {
            ID = int.Parse(id.ToString());
        }
        if (obj is string)
        {
            value = double.Parse(obj.ToString());
        }
        Loom.QueueOnMainThread((param) =>
        {

            // id=Convert.ToInt32(id) ;
            // obj=Convert.ToDouble(obj);
            // id = int.Parse(id.ToString());
            // obj = double.Parse(obj.ToString());
            switch ((AngleSensorRequest.SENSOR_ID)ID)
            {
                case AngleSensorRequest.SENSOR_ID.SENSOR_1:
                    break;
                case AngleSensorRequest.SENSOR_ID.SENSOR_2://= -0.1084x + 251.88
                    jianxuanzhuan.localEulerAngles = new Vector3(0, (-0.1084f + float.Parse(inputFields[0].text)) * (float)value + 251.88f + 30f * float.Parse(inputFields[5].text), 0);
                    break;
                case AngleSensorRequest.SENSOR_ID.SENSOR_3://= -0.106x + 179.21
                    shangbi0.localEulerAngles = new Vector3((-0.106f + float.Parse(inputFields[1].text)) * (float)value + 179.21f + 30f * float.Parse(inputFields[6].text), 0, 0);
                    shangbi1.localEulerAngles = shangbi0.localEulerAngles;
                    zhouguanjie1.localEulerAngles = -1 * shangbi0.localEulerAngles;

                    break;
                case AngleSensorRequest.SENSOR_ID.SENSOR_4://= -0.1084x + 177.83
                    zhouguanjie0.localEulerAngles = new Vector3(0, (-0.1084f + float.Parse(inputFields[2].text)) * (float)value + 177.83f + 30 * float.Parse(inputFields[7].text), 0);
                    break;
                case AngleSensorRequest.SENSOR_ID.SENSOR_5://= 0.1102x - 178.06
                    xiabi.localEulerAngles = new Vector3((0.1102f + float.Parse(inputFields[3].text)) * (float)value - 178.06f - 30f * float.Parse(inputFields[8].text), 0, 0);
                    break;
                case AngleSensorRequest.SENSOR_ID.SENSOR_6://= -0.1392x + 269.47
                    shouwan.localEulerAngles = new Vector3(0, 0, (-0.1392f + float.Parse(inputFields[4].text)) * (float)value + 269.47f + 30 * float.Parse(inputFields[9].text));
                    break;
                case AngleSensorRequest.SENSOR_ID.SENSOR_7:
                    break;
                default:
                    break;
            }
        }, null);

    }


}
